package org.jeecg.modules.fk.core.rest;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import org.jeecg.modules.fk.core.rest.response.*;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

@JsonInclude(Include.NON_NULL)
public class RestResponse<T> implements Serializable {
    private static final String CODE_SUCCESS = "0";
    private static final long serialVersionUID = 4572910611616655490L;
    private RestResponseHead head;
    private T body;
    private Map<String, Object> extMap;
    @JsonIgnore
    private Object[] errorParams;

    public static <T> RestResponse<T> successData(T data) {
        return new RestResponse(data);
    }

    public static <T> RestResponse<T> successMessage(String message) {
        return new RestResponse("0", message);
    }

    public static <T> RestResponse<T> success(String message, T data) {
        return new RestResponse(message, data);
    }

    public static <T> RestResponse<T> error(String errorCode, Object... errorParams) {
        RestResponse<T> response = new RestResponse(errorCode, errorParams[0].toString());
        response.setErrorParams(errorParams);
        return response;
    }

    public static RestResponse<String> redirect(String url) {
        RestResponse<String> response = new RestResponse((String) null, "302", (String) null, url);
        return response;
    }

    public RestResponse() {
    }

    public RestResponse(String errorCode, String errorMsg) {
        this((String) null, errorCode, errorMsg, null, 0L);
    }

    public RestResponse(T body) {
        this((String) null, "0", (String) null, body, 0L);
    }

    public RestResponse(String errorMsg, T body) {
        this((String) null, "0", errorMsg, body, 0L);
    }

    public RestResponse(String errorCode, String errorMsg, T body) {
        this((String) null, errorCode, errorMsg, body, 0L);
    }

    public RestResponse(String requestId, String errorCode, String errorMsg) {
        this(requestId, errorCode, errorMsg, null, 0L);
    }

    public RestResponse(String requestId, String errorCode, String errorMsg, T body) {
        this(requestId, errorCode, errorMsg, body, 0L);
    }

    public RestResponse(String requestId, String errorCode, String errorMsg, T body, long time) {
        this(requestId, errorCode, (String) null, errorMsg, body, time);
    }

    public RestResponse(String requestId, String errorCode, String errorNo, String errorMsg, T body, long time) {
        this.head = new RestResponseHead(requestId, errorCode, errorMsg, System.currentTimeMillis(), time);
        this.head.setErrorNo(errorNo);
        this.body = body;
    }

    public RestResponse(RestResponseHead head, T body) {
        this.head = head;
        this.body = body;
    }

    public RestResponseHead getHead() {
        return this.head;
    }

    public void setHead(RestResponseHead head) {
        this.head = head;
    }

    public T getBody() {
        return this.body;
    }

    public void setBody(T body) {
        this.body = body;
    }

    @JsonIgnore
    public boolean isSuccess() {
        return this.head.isSuccess();
    }

    @JsonIgnore
    public boolean isError() {
        return this.head.isError();
    }

    @JsonIgnore
    public Object getData() {
        return this.body != null && this.body instanceof Map && ((Map) this.body).get("pager") != null ? ((Map) this.body).get("data") : this.body;
    }

    public void putExtObject(String key, Object obj) {
        if (this.extMap == null) {
            this.extMap = new HashMap();
        }

        this.extMap.put(key, obj);
    }

    public Object getExtObject(String key) {
        return this.extMap == null ? null : this.extMap.get(key);
    }

    public void removeExtObject(String key) {
        if (this.extMap != null) {
            this.extMap.remove(key);
        }
    }

    public Map<String, Object> getExtMap() {
        return this.extMap;
    }

    public void setExtMap(Map<String, Object> extMap) {
        this.extMap = extMap;
    }

    public Object[] getErrorParams() {
        return this.errorParams;
    }

    public void setErrorParams(Object[] errorParams) {
        this.errorParams = errorParams;
    }

    public static RestResponse<StringResponse> create(String key, String value) {
        return create(key, (Object) value, (Class) StringResponse.class);
    }

    public static RestResponse<IntegerResponse> create(String key, Integer value) {
        return create(key, (Object) value, (Class) IntegerResponse.class);
    }

    public static RestResponse<LongResponse> create(String key, Long value) {
        return create(key, (Object) value, (Class) LongResponse.class);
    }

    public static RestResponse<BooleanResponse> create(String msg, String key, Boolean value) {
        return create(msg, key, value, BooleanResponse.class);
    }

    public static RestResponse<StringResponse> create(String msg, String key, String value) {
        return create(msg, key, value, StringResponse.class);
    }

    public static RestResponse<IntegerResponse> create(String msg, String key, Integer value) {
        return create(msg, key, value, IntegerResponse.class);
    }

    public static RestResponse<LongResponse> create(String msg, String key, Long value) {
        return create(msg, key, value, LongResponse.class);
    }

    public static RestResponse<BooleanResponse> create(String key, Boolean value) {
        return create(key, (Object) value, (Class) BooleanResponse.class);
    }

    private static <T> RestResponse<T> create(String key, Object value, Class<T> klass) {
        return create("OK", key, value, klass);
    }

    private static <T> RestResponse<T> create(String msg, String key, Object value, Class<T> klass) {
        OneKeyResponse response;
        try {
            response = (OneKeyResponse) klass.newInstance();
        } catch (IllegalAccessException | InstantiationException var6) {
            throw new IllegalStateException("实例化失败", var6);
        }

        response.singleton(key, value);
        return success(msg, (T)response);
    }
}
